Coverage for cpprb/VectorWrapper.pyx: 73%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

84 statements  

1# distutils: language = c++ 

2# cython: linetrace=True 

3  

4cimport numpy as np 

5import numpy as np 

6  

7from libc.stdlib cimport malloc, free 

8from cython.operator cimport dereference 

9import cython 

10  

11cdef class VectorWrapper: 

12 def __cinit__(self,ndim=1,value_dim=1,**kwarg): 

13 self.ndim = min(ndim,2) 

14 self.value_dim = value_dim 

15  

16 self.shape = <Py_ssize_t*>malloc(sizeof(Py_ssize_t) * self.ndim) 

17 self.strides = <Py_ssize_t*>malloc(sizeof(Py_ssize_t) * self.ndim) 

18  

19 def __reduce__(self): 

20 return (self.__class__, (self.ndim,self.value_dim)) 

21  

22 cdef void update_size(self): 

23 self.shape[0] = <Py_ssize_t>(self.vec_size()//self.value_dim) 

24  

25 cdef void set_buffer(self,Py_buffer* buffer): 

26 raise NotImplementedError() 

27  

28 cdef void set_shape_strides(self): 

29 self.strides[self.ndim -1] = self.itemsize 

30  

31 if self.ndim == 2: 

32 self.shape[1] = <Py_ssize_t> (self.value_dim) 

33 self.strides[0] = self.value_dim * self.itemsize 

34  

35 cpdef size_t vec_size(self): 

36 raise NotImplementedError() 

37  

38 def __dealloc__(self): 

39 free(self.shape) 

40 free(self.strides) 

41  

42 def __getbuffer__(self, Py_buffer *buffer, int flags): 

43 self.update_size() 

44  

45 self.set_buffer(buffer) 

46 buffer.len = self.vec_size() * self.itemsize 

47 buffer.readonly = 0 

48 buffer.ndim = self.ndim 

49 buffer.shape = self.shape 

50 buffer.strides = self.strides 

51 buffer.suboffsets = NULL 

52 buffer.itemsize = self.itemsize 

53 buffer.internal = NULL 

54 buffer.obj = self 

55  

56 def __releasebuffer__(self, Py_buffer *buffer): 

57 pass 

58  

59 def as_numpy(self,copy = False,**kwargs): 

60 return np.array(self,copy=copy,**kwargs) 

61  

62cdef class VectorInt(VectorWrapper): 

63 def __cinit__(self,ndim=1,value_dim=1,**kwargs): 

64 self.vec = vector[int]() 

65 self.itemsize = sizeof(int) 

66 self.set_shape_strides() 

67  

68 cdef void set_buffer(self,Py_buffer* buffer): 

69 buffer.buf = <void*>(self.vec.data()) 

70 buffer.format = 'i' 

71  

72 cpdef size_t vec_size(self): 

73 return self.vec.size() 

74  

75cdef class VectorDouble(VectorWrapper): 

76 def __cinit__(self,ndim=1,value_dim=1,**kwargs): 

77 self.vec = vector[double]() 

78 self.itemsize = sizeof(double) 

79 self.set_shape_strides() 

80  

81 cdef void set_buffer(self,Py_buffer* buffer): 

82 buffer.buf = <void*>(self.vec.data()) 

83 buffer.format = 'd' 

84  

85 cpdef size_t vec_size(self): 

86 return self.vec.size() 

87  

88cdef class VectorFloat(VectorWrapper): 

89 def __cinit__(self,ndim=1,value_dim=1,**kwargs): 

90 self.vec = vector[float]() 

91 self.itemsize = sizeof(float) 

92 self.set_shape_strides() 

93  

94 cdef void set_buffer(self,Py_buffer* buffer): 

95 buffer.buf = <void*>(self.vec.data()) 

96 buffer.format = 'f' 

97  

98 cpdef size_t vec_size(self): 

99 return self.vec.size() 

100  

101cdef class VectorSize_t(VectorWrapper): 

102 def __cinit__(self,ndim=1,value_dim=1,**kwargs): 

103 self.vec = vector[size_t]() 

104 self.itemsize = sizeof(size_t) 

105 self.set_shape_strides() 

106  

107 cdef void set_buffer(self,Py_buffer* buffer): 

108 buffer.buf = <void*>(self.vec.data()) 

109 if sizeof(size_t) == sizeof(unsigned long): 

110 buffer.format = 'L' 

111 elif sizeof(size_t) == sizeof(unsigned long long): 

112 buffer.format = 'Q' 

113 elif sizeof(size_t) == sizeof(unsigned int): 

114 buffer.format = 'I' 

115 elif sizeof(size_t) == sizeof(unsigned char): 

116 buffer.format = 'B' 

117 else: 

118 raise BufferError("Unknown size_t implementation!") 

119  

120 cpdef size_t vec_size(self): 

121 return self.vec.size() 

122  

123cdef class PointerDouble(VectorWrapper): 

124 def __cinit__(self,ndim=1,value_dim=1,size=1,**kwargs): 

125 self._vec_size = value_dim * size 

126 self.itemsize = sizeof(double) 

127 self.set_shape_strides() 

128  

129 def __reduce__(self): 

130 return (self.__class__, 

131 (self.ndim,self.value_dim,self._vec_size // self.value_dim)) 

132  

133 cdef void set_buffer(self,Py_buffer* buffer): 

134 buffer.buf = <void*> self.ptr 

135 buffer.format = 'd' 

136  

137 cdef void update_vec_size(self,size_t size): 

138 self._vec_size = self.value_dim * size 

139  

140 cpdef size_t vec_size(self): 

141 return self._vec_size